#include "sdk.h"
#include "edittooldlg.h"
#include <wx/filedlg.h>

BEGIN_EVENT_TABLE( EditToolDlg, wxScrollingDialog )
  EVT_BUTTON( XRCID( "btnBrowseCommand" ), EditToolDlg::OnBrowseCommand )
  EVT_BUTTON( XRCID( "btnBrowseDir" ), EditToolDlg::OnBrowseDir )
  EVT_UPDATE_UI( -1, EditToolDlg::OnUpdateUI )
END_EVENT_TABLE()

EditToolDlg::EditToolDlg( wxWindow* parent, cbTool* tool )
  : m_Tool( tool ) {
  if( !tool ) {
    m_Throw( _T( "Tool* 参数在中是必需的 EditToolDlg()" ) );
  }
  wxXmlResource* xml_res = wxXmlResource::Get();
  wxString resPath = ConfigManager::GetDataFolder() + _T( "/配置工具参数.xrc" );
  if( !xml_res->Load( resPath ) )  {
    xml_File_lock = false;
    Manager::Get()->GetLogManager()->LogInf( F( _T( "文件丢失: %s " ), resPath.wx_str() ) );
    return;
  }
  if( !xml_res->LoadObject( this, parent, _T( "dlgEditTool" ), _T( "wxScrollingDialog" ) ) ) {
    xml_File_lock = false;
    Manager::Get()->GetLogManager()->LogInf( _T( "资源丢失" ) );
    return;
  }
  xml_File_lock = true;
  XRCCTRL( *this, "wxID_OK", wxButton )->SetDefault();
  XRCCTRL( *this, "txtName", wxTextCtrl )->SetValue( m_Tool->GetName() );
  XRCCTRL( *this, "txtCommand", wxTextCtrl )->SetValue( m_Tool->GetCommand() );
  XRCCTRL( *this, "txtParams", wxTextCtrl )->SetValue( m_Tool->GetParams() );
  XRCCTRL( *this, "txtDir", wxTextCtrl )->SetValue( m_Tool->GetWorkingDir() );
  XRCCTRL( *this, "rbLaunchOptions", wxRadioBox )->SetSelection( static_cast<int>( m_Tool->GetLaunchOption() ) );
}

EditToolDlg::~EditToolDlg() {
}

void EditToolDlg::OnUpdateUI( M_Code_unused wxUpdateUIEvent& event ) {
  const wxString name = XRCCTRL( *this, "txtName", wxTextCtrl )->GetValue();
  const wxString command = XRCCTRL( *this, "txtCommand", wxTextCtrl )->GetValue();
  bool en = !name.IsEmpty() && !command.IsEmpty();
  XRCCTRL( *this, "wxID_OK", wxButton )->Enable( en );
}

void EditToolDlg::OnBrowseCommand( M_Code_unused wxCommandEvent& event ) {
  const wxFileName file( XRCCTRL( *this, "txtCommand", wxTextCtrl )->GetValue() );
  wxString filename = wxFileSelector( _T( "选择可执行文件" ), file.GetPath( wxPATH_GET_VOLUME ), file.GetFullName() );
  if( !filename.IsEmpty() ) {
    XRCCTRL( *this, "txtCommand", wxTextCtrl )->SetValue( filename );
  }
}

void EditToolDlg::OnBrowseDir( M_Code_unused wxCommandEvent& event ) {
  const wxString dir = ChooseDirectory( this, _T( "选择工作目录" ), XRCCTRL( *this, "txtDir", wxTextCtrl )->GetValue() );
  if( !dir.IsEmpty() ) {
    XRCCTRL( *this, "txtDir", wxTextCtrl )->SetValue( dir );
  }
}

void EditToolDlg::EndModal( int retCode ) {
  if( retCode == wxID_OK ) {
    m_Tool->SetName( XRCCTRL( *this, "txtName", wxTextCtrl )->GetValue() );
    m_Tool->SetCommand( XRCCTRL( *this, "txtCommand", wxTextCtrl )->GetValue() );
    m_Tool->SetParams( XRCCTRL( *this, "txtParams", wxTextCtrl )->GetValue() );
    m_Tool->SetWorkingDir( XRCCTRL( *this, "txtDir", wxTextCtrl )->GetValue() );
    m_Tool->SetLaunchOption( static_cast<cbTool::eLaunchOption>( XRCCTRL( *this, "rbLaunchOptions", wxRadioBox )->GetSelection() ) );
  }
  wxScrollingDialog::EndModal( retCode );
}
